/* 
 * File:   ResourceManager.cpp
 * Author: RedEyedKiller
 * 
 * Created on 22 Ιούνιος 2011, 1:39 πμ
 */

#include "ResourceManager.h"
#include "Globals.h"
#include "ResourceManager.h"

SingletonInitialization(ResourceManager)

ResourceManager::~ResourceManager()
{
}

void ResourceManager::InitializePools()
{
    imagePool.SetPath( Globals::GetInstance( )->GetPath( "Images" ) );
    texturePool.SetPath( Globals::GetInstance( )->GetPath( "Images" ) );
    fontPool.SetPath( Globals::GetInstance( )->GetPath( "Fonts" ) );
    soundPool.SetPath( Globals::GetInstance( )->GetPath( "Sounds" ) );
}

Resource<sdl::Image>* ResourceManager::GetImage(const char* name)
{
    Loader<sdl::Image> loader;
    loader.request = name;
    return imagePool.RequestResource( loader );
}

Resource<sdl::Image>* ResourceManager::GetImage(const char* path, const char* name)
{
    Loader<sdl::Image> loader;
    loader.request = name;
    loader.path = path;
    return imagePool.RequestResource( loader );
}

Resource<gl::Texture>* ResourceManager::GetTexture(const char* name)
{
    Loader<gl::Texture> loader;
    loader.request = name;
    return texturePool.RequestResource( loader );
}

Resource<gl::Texture>* ResourceManager::GetTexture(const char* path, const char* name)
{
    Loader<gl::Texture> loader;
    loader.request = name;
    loader.path = path;
    return texturePool.RequestResource( loader );
}

Resource<al::Sound>* ResourceManager::GetSound(const char* name)
{
    Loader<al::Sound> loader;
    loader.request = name;
    Resource<al::Sound>* r = soundPool.RequestResource( loader );
    if( r )
    {
        r->SetImportant( );
    }
    return r;
}

Resource<al::Sound>* ResourceManager::GetSound(const char* path, const char* name)
{
    Loader<al::Sound> loader;
    loader.request = name;
    loader.path = path;
    Resource<al::Sound>* r = soundPool.RequestResource( loader );
    if( r )
    {
        r->SetImportant( );
    }
    return r;
}

Resource<sdl::Font>* ResourceManager::GetFont(const char* name, int size)
{
    Loader<sdl::Font> loader;
    loader.request = name;
    loader.size = size;
    return fontPool.RequestResource( loader );
}

Resource<sdl::Font>* ResourceManager::GetFont(const char* path, const char* name, int size)
{
    Loader<sdl::Font> loader;
    loader.request = name;
    loader.size = size;
    loader.path = path;
    return fontPool.RequestResource( loader );
}

/**
 * Garbage collects all unused resources.
 * @param killEmAll if true important resources will be collected as well if they have no references.
 */
void ResourceManager::GarbageCollect(bool killEmAll /*= false*/)
{
    imagePool.GarbageCollection( killEmAll );
    texturePool.GarbageCollection( killEmAll );
    fontPool.GarbageCollection( killEmAll );
    soundPool.GarbageCollection( killEmAll );
}